<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\data\pic;

use app\enum\AnalysisEnum;
use app\enum\CommonEnum;
use app\model\mongo\MongoClientAccountTypeDayStatModel;
use app\model\mongo\MongoReportModel;
use app\model\mongo\MongoStatModel;
use app\service\analysis\AnalysisService;
use app\service\analysis\report\ReportPicService;
use app\service\BaseService;
use think\Model;

/**
 * 图片-按客户+账户+素材汇总.
 */
final class AccountPicStatService extends BaseService
{
    /**
     * @var int 媒体id
     */
    private int $mediaId;

    /**
     * @var string 日报主键
     */
    private string $objectId;

    /**
     * @var null|ReportPicService 账户服务
     */
    private ?ReportPicService $service;

    /**
     * @var null|MongoReportModel 日报模型
     */
    private ?MongoReportModel $object;

    public function __construct()
    {
    }

    /**
     * 执行任务
     * @param array $arr 请求参数
     */
    public function runTask(array $arr): bool
    {
        $this->objectId = strval($arr[0] ?? '');
        $this->mediaId = intval($arr[1] ?? 0);

        if (! $this->checkProperty()) {
            return false;
        }

        $this->service = new ReportPicService($this->mediaId);
        if (! $this->service->isService()) {
            return false;
        }

        if (! $this->getObjectModel()) {
            return false;
        }

        return $this->setStat();
    }

    /**
     * 属性检查.
     */
    private function checkProperty(): bool
    {
        if (empty($this->mediaId)) {
            return self::setError('未知媒体');
        }
        if (empty($this->objectId)) {
            return self::setError('未知主键id');
        }
        return true;
    }

    /**
     * 获取日报数据模型.
     */
    private function getObjectModel(): bool
    {
        $this->object = $this->service->getModelById($this->objectId);
        if (! $this->object->getKey()) {
            return self::setError('未知日报数据');
        }
        return true;
    }

    /**
     * 设置汇总数据.
     */
    private function setStat(): bool
    {
        $model = $this->getStatModel();
        if (! $model->getKey()) {
            $data = [
                'company_id' => $this->object->getAttr('company_id'),
                'client_id' => $this->object->getAttr('client_id'),
                'account_id' => $this->object->getAttr('account_id'),
                'adv_id' => $this->object->getAttr('adv_id'),
                'pic_id' => $this->object->getAttr('pic_id'),
                'signature' => $this->object->getAttr('signature'),
                'is_change' => MongoStatModel::IS_CHANGE_YES,
            ];
            if ($this->mediaId == CommonEnum::KS_MEDIA) {
                $data['user_id'] = $this->object->getAttr('user_id');
            }
        } else {
            $data = [
                'is_change' => MongoStatModel::IS_CHANGE_YES,
            ];
        }
        $data = $this->getStatField($data);
        $model->setAttrs($data);
        if (! $model->save()) {
            return self::setError('保存汇总数据失败');
        }
        return true;
    }

    /**
     * 获取汇总模型.
     *
     * @return array|mixed|Model|MongoClientAccountTypeDayStatModel
     */
    private function getStatModel()
    {
        $model = AnalysisService::getStatModelList($this->mediaId, AnalysisEnum::TYPE_PIC, AnalysisEnum::STAT_ACCOUNT);
        return $model->where([
            'account_id' => $this->object->getAttr('account_id'),
            'pic_id' => $this->object->getAttr('pic_id'),
        ])
            ->findOrEmpty();
    }

    /**
     * 获取汇总字段值
     * @param array $data 数据值
     */
    private function getStatField(array $data = []): array
    {
        $map = [
            'account_id' => $this->object->getAttr('account_id'),
            'pic_id' => $this->object->getAttr('pic_id'),
        ];
        $stat = $this->service->getAccountStat($map);
        $data['cost'] = $stat['cost'] ?? 0;
        return $data;
    }
}
